package com.vision.shop.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.vision.shop.dao.IUserDao;
import com.vision.shop.model.Pager;
import com.vision.shop.model.ShopDi;
import com.vision.shop.model.ShopException;
import com.vision.shop.model.User;
import com.vision.shop.util.RequestUtil;

public class UserServlet extends BaseServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IUserDao userDao;
	@ShopDi
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public String list(HttpServletRequest req,HttpServletResponse resp){
		Pager<User> users=userDao.find("");
		req.setAttribute("users", users);
		return "user/list.jsp";
	}
	@Auth("any")
	public String addInput(HttpServletRequest req,HttpServletResponse resp){
		return "user/addInput.jsp";
	}
	
	public String delete(HttpServletRequest req,HttpServletResponse resp){
		int id=Integer.parseInt(req.getParameter("id"));
		userDao.delete(id);
		return "redirect:/user.do?method=list";
	}
	public String updateInput(HttpServletRequest req,HttpServletResponse resp){
		int id=Integer.parseInt(req.getParameter("id"));
		User u=userDao.load(id);
		req.setAttribute("user", u);
		return "user/updateInput.jsp";
	}
	public String update(HttpServletRequest req,HttpServletResponse resp){
		User u=(User)RequestUtil.setParam(User.class, req);
		/**
		 * 验证
		 */
		Boolean isValidate=RequestUtil.validate(User.class, req);
		if(!isValidate){
			return "user/updateInput.jsp";
		}
		int id=Integer.parseInt(req.getParameter("id"));
		User user=userDao.load(id);
		user.setNickname(u.getNickname());
		user.setPassword(u.getPassword());
		userDao.update(user);
		return "redirect:/user.do?method=list";
	}
	public String changeType(HttpServletRequest req,HttpServletResponse resp){
		int id=Integer.parseInt(req.getParameter("id"));
		User u=userDao.load(id);
		if(u.getType()==0){
			u.setType(1);
		}else{
			u.setType(0);
		}
		userDao.update(u);
		return "redirect:/user.do?method=list";
	}
	@Auth("any")
	public String add(HttpServletRequest req,HttpServletResponse resp){
		User u=(User)RequestUtil.setParam(User.class, req);
		Boolean isValidate=RequestUtil.validate(User.class, req);
		if(!isValidate){
			return "user/addInput.jsp";
		}
		try {
			userDao.add(u);
		} catch (ShopException e) {
			req.setAttribute("errorMsg", e.getMessage());
			return "inc/error.jsp";
		}
		return "redirect:/user.do?method=list";
		/*String username=req.getParameter("username");
		String password=req.getParameter("password");
		String nickname=req.getParameter("nickname");
		User u=new User();
		u.setNickname(nickname);
		u.setPassword(password);
		u.setUsername(username);
		userDao.add(u);*/
		/*第二种方法
		 * try {
			Map<String,String[]> params=req.getParameterMap();
			Set<String> keys=params.keySet();
			User u=new User();
			for(String key:keys){
				String mk="set"+key.substring(0,1).toUpperCase()+key.substring(1);
				System.out.println(mk);
				try {
					Method m= u.getClass().getDeclaredMethod(mk, String.class);
					m.invoke(u, params.get(key)[0]);
				} catch (NoSuchMethodException e) {
					continue;
				}	
			}
			userDao.add(u);	
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}*/
		
		/*Map<String,String> errors=new HashMap<String,String>();
		req.setAttribute("errors", errors);
		if(u.getUsername()==null||"".equals(u.getNickname())){
			errors.put("username", "用户名不能为空");
			return "user/addInput.jsp";
		}
		*/	
	}
	@Auth("any")
	public String loginInput(HttpServletRequest req,HttpServletResponse resp){
		
		return "user/loginInput.jsp";
	}
	@Auth("any")
	public String login(HttpServletRequest req,HttpServletResponse resp){
		String username=req.getParameter("username");
		String password=req.getParameter("password");
		try {
			User u=userDao.login(username, password);
			req.getSession().setAttribute("loginUser", u);
		} catch (ShopException e) {
			return this.handleException(e, req);
		}
		return "redirect:/product.do?method=list";
	}
	@Auth("any")
	public String logout(HttpServletRequest req,HttpServletResponse resp){
		req.getSession().invalidate();
		return "redirect:/user.do?method=loginInput";
	}
	@Auth
	public String updateSelfInput(HttpServletRequest req,HttpServletResponse resp){
		User u=(User)req.getSession().getAttribute("loginUser");
		req.setAttribute("user", u);
		return "user/updateSelfInput.jsp";
	}
	@Auth
	public String updateSelf(HttpServletRequest req,HttpServletResponse resp){
		User tu=(User)RequestUtil.setParam(User.class, req);
		/**
		 * 验证
		 */
		Boolean isValidate=RequestUtil.validate(User.class, req);
		if(!isValidate){
			return "user/updateSelfInput.jsp";
		}
		User user=(User)req.getSession().getAttribute("loginUser");
		user.setNickname(tu.getNickname());
		user.setPassword(tu.getPassword());
		userDao.update(user);
		return "redirect:/goods.do?method=list";
	}
	@Auth
	public String show(HttpServletRequest req,HttpServletResponse resp){
		User user=userDao.load(Integer.parseInt(req.getParameter("id")));
		req.setAttribute("user", user);
		return "user/show.jsp";
	}
}
